package dao

import (
	"context"
	"encoding/json"
	"fmt"
	"server/basic/config"
	"server/basic/model"
	"time"
)

func RedisLogin(mobile string, code int32) bool {
	key := fmt.Sprintf("userlogin%s", mobile)
	val, _ := config.Rdb.Get(context.Background(), key).Int()
	if val != int(code) {
		return false
	}
	return true
}

func RedisSuccess(mobile string, code int) bool {
	key := fmt.Sprintf("userlogin%s", mobile)
	err := config.Rdb.Set(context.Background(), key, code, 10*time.Minute).Err()
	if err != nil {
		return false
	}
	return true
}

func RedisHospitalListM() (hospital string, err error) {
	key := fmt.Sprintf("HospitalListM")
	result, err := config.Rdb.Get(context.Background(), key).Result()
	if err != nil {
		return "", err
	}
	return result, nil

}

func RedisHospitalListMAdd(add []model.Hospitals) bool {
	json.Marshal(add)
	key := fmt.Sprintf("HospitalListM")

	err := config.Rdb.Set(context.Background(), key, add, 30*time.Minute).Err()
	if err != nil {
		return false
	}
	return true
}

func RedisHospitalList(page int32, size int32) (hospital string, err error) {
	key := fmt.Sprintf("HospitalList:%d:%d", page, size)
	result, err := config.Rdb.Get(context.Background(), key).Result()
	if err != nil {
		return "", err
	}
	return result, nil
}

func RedisHospitalListAdd(hospitals []model.Hospitals, page int32, size int32) bool {
	json.Marshal(hospitals)
	key := fmt.Sprintf("HospitalList:%d:%d", page, size)

	err := config.Rdb.Set(context.Background(), key, hospitals, 30*time.Minute).Err()
	if err != nil {
		return false
	}
	return true
}
